package com.example.openguass.controller;


import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.Customer;
import com.example.openguass.entity.DishFlavor;
import com.example.openguass.service.DishFlavorService;
import com.example.openguass.service.DishService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.*;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/dish-flavor")
@Api(value = "DishFlavorController", tags = "菜品口味接口")
public class DishFlavorController {
    private DishFlavorService dishFlavorService;
    private DishService dishService;
    @Autowired
    private void setDishFlavorService(DishFlavorService dishFlavorService){
        this.dishFlavorService = dishFlavorService;
    }
    @Autowired
    private void setDishService(DishService dishService){
        this.dishService = dishService;
    }

    @PostMapping("insert")
    public CommonResult insert(@RequestBody DishFlavorVO dishFlavorVO){
        CommonResult result = new CommonResult().init();
        if (dishFlavorService.exit(dishFlavorVO)){
            return (CommonResult) result.failCustom(-10086,"当前菜品的口味已存在");
        }
        if (dishFlavorService.insert(dishFlavorVO)){
            return result.success("创建口味成功",dishFlavorVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"创建口味失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) DishFlavorVO dishFlavorVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (dishFlavorService.getById(dishFlavorVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前口味");
        }
        if (dishFlavorService.update(dishFlavorVO)){
            return result.success("dishFlavorVO",dishFlavorVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新口味失败");
        }
    }

    @GetMapping("/delete/{dishFlavorId}")
    public CommonResult delete(@PathVariable String dishFlavorId){
        CommonResult result = new CommonResult().init();
        if (dishFlavorService.getById(dishFlavorId) == null){
            return (CommonResult) result.failCustom(-10086,"该菜品口味不存在");
        }
        if (dishFlavorService.removeById(dishFlavorId)){
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除菜品口味失败");
        }
    }

    @GetMapping("/select/{dishFlavorId}")
    public CommonResult select(@PathVariable String dishFlavorId){
        CommonResult result = new CommonResult().init();
        if (dishFlavorService.getById(dishFlavorId) == null){
            return (CommonResult) result.failCustom(-10086,"该菜品口味不存在");
        }
        DishFlavor dishFlavor = dishFlavorService.getById(dishFlavorId);
        DishFlavorReturnVO dishFlavorReturnVO = new DishFlavorReturnVO();
        BeanUtils.copyProperties(dishFlavor,dishFlavorReturnVO);
        dishFlavorReturnVO.setDishName(dishService.getById(dishFlavor.getDishId()).getName());
        return result.success("dishFlavor",dishFlavorReturnVO);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody DishFlavorPageVO dishFlavorPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        return (CommonResult) result.success("page",dishFlavorService.findPage(dishFlavorPageVO)).end();
    }
}
